<?php
$cPublisherID   =   'K380716';
$cScope         =   'https://www.googleapis.com/auth/gan.readonly';
$cClientID      =   '126738922261.apps.googleusercontent.com';
$cClientSecret  =   'aT_kmncInPPNVCxEMidx5h58';
$cRedirectURI   =   'urn:ietf:wg:oauth:2.0:oob';
 
$cAuthCode      =   '4/-glVsDkn__oT4wlRYiA-aXAYwLzx.0lZvj161EkgcuJJVnL49Cc9MKl_rcgI';
$cRefreshToken  =   '1/sSIZzjCPCOs5fjFeFgArKNYo4m35cyv-_rXKmqCi4Y8';
 
$bRefresh = true;
 
if (empty($cAuthCode)) {
    $rsParams = array(
                        'response_type' =>   'code',
                        'client_id'     =>   $cClientID,
                        'redirect_uri'  =>   $cRedirectURI,
                        'scope'         =>   $cScope
                        );
    $cOauthURL = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($rsParams);
    echo("Go to\n$cOauthURL\nand enter the given value into \$cAuthCode\n");
    exit();
} // ends if (empty($cAuthCode))
elseif (empty($cRefreshToken)) {
    $cTokenURL = 'https://accounts.google.com/o/oauth2/token';
    $rsPostData = array(
                        'code'          =>   $cAuthCode,
                        'client_id'     =>   $cClientID,
                        'client_secret' =>   $cClientSecret,
                        'redirect_uri'  =>   $cRedirectURI,
                        'grant_type'    =>   'authorization_code',
                        );
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_URL, $cTokenURL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $rsPostData);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
    $cTokenReturn = curl_exec($ch);
    $oToken = json_decode($cTokenReturn);
    echo("Enter the following values:\n\n");
    echo("\$cRefreshToken = '" . $oToken->refresh_token . "';\n");
} // ends
else {
    // Get a new Access Token
    $cTokenURL = 'https://accounts.google.com/o/oauth2/token';
    $rsPostData = array(
                        'client_secret' =>   $cClientSecret,
                        'grant_type'    =>   'refresh_token',
                        'refresh_token' =>   $cRefreshToken,
                        'client_id'     =>   $cClientID
                        );
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_URL, $cTokenURL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $rsPostData);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
    $cTokenReturn 	= curl_exec($ch);
    $oToken 		= json_decode($cTokenReturn);
    $cAccessToken 	= $oToken->access_token;
 
 	$advertiserId	= '48080';
 	$startDateMin	= "2012-08-15";
 	
 	while (true) {
 		echo "======================================= ";
 		echo "\r\n";
 		
	    // Get the results
	    $cGANURL = 'https://www.googleapis.com/gan/v1beta1/publishers/' . $cPublisherID . '/links?maxResults=2';
	    
	    if(!empty($startDateMin)){
	    	$cGANURL .= '&startDateMin=' . $startDateMin . 'T00:00:01.000Z';
	    }
	    
	    if(!empty($advertiserId)){
	    	$cGANURL .= '&advertiserId=' . $advertiserId;
	    }
	    
	    if (isset($cJson->nextPageToken)) 
	 		$cGANURL .= '&pageToken=' . $cJson->nextPageToken;

	    $cAuthHeader = "Authorization: OAuth " . $cAccessToken;	 		
	 
	 	echo "==cGANURL===$cGANURL ";
	 	echo "\r\n";
	 	
	    $ch = curl_init();
	 
	    curl_setopt($ch, CURLOPT_HTTPHEADER, array($cAuthHeader));
	    curl_setopt($ch, CURLOPT_URL, $cGANURL);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	 
	    $cJsonReturn 	= curl_exec($ch);

	 	$cJson = json_decode($cJsonReturn);
	 	
	 			
		echo '==next token: ' . $cJson->nextPageToken;
		echo "\r\n";
		

	    print_r($cJson);
	    
	    
	    if (!isset($cJson->nextPageToken))
	 		break;

	}
	
} // ends else from <all authenticated>
 
?>